package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


@RequestMapping("/banner")
@Controller
public class BannerController {
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") BannerQuery qo){
        //根据请求查询分页数据
        Page<Banner> page = bannerService.query(qo);
        //加到模型中
        model.addAttribute("page", page);
        return "banner/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        //调用服务层方法
        if (StringUtils.hasLength(banner.getId())){
            //调用更新方法
            bannerService.update(banner);
        }else {
            //调用保存方法
            bannerService.save(banner);
        }
        //返回执行成功
        return JsonResult.success();
    }
    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType(Model model){
        //将攻略和游记也加进去
        Map<String,Object> map=new HashMap<>();
        map.put("ts", travelService.selectAll());
        map.put("sts", strategyService.selectAll());
        return JsonResult.success(map);
    }
}
